package com.ruoyi.framework.security.handle;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginType;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.security.service.UserDetailsServiceImpl;
import com.ruoyi.framework.weChat.social.BaseSocialUser;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysUser;
import org.apache.catalina.security.SecurityUtil;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.social.security.SocialUser;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Classname BaseAuthenticationSuccessHandler
 * @Description 登录成功处理器
 * @Author h21448-hxtq
 * @Date 2020-04-01 17:37
 * @Version 1.0
 */
@Component
public class BaseAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Value("${baseframe.url.address}")
    private String url;
    @Autowired
    TokenService tokenService;
    @Autowired
    UserDetailsServiceImpl userDetailsService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Object principal = authentication.getPrincipal();
        if (principal instanceof LoginUser) {
            LoginUser userDetail = (LoginUser) authentication.getPrincipal();
            //存储认证信息
            SecurityContextHolder.getContext().setAuthentication(authentication);
            //生成token
            String s = tokenService.createToken(userDetail); //JwtUtil.generateToken(userDetail);
            // 是短信登录返回token
            if (LoginType.sms.equals(userDetail.getLoginType())) {
                if (StrUtil.isEmpty(s)) {
                    throw new CustomException("登录失败");
                }
                AjaxResult ajaxResult=new AjaxResult();
                ajaxResult.put(Constants.TOKEN,s);
                ajaxResult.put("code",200);
                SecurityUtils.writeJavaScript(ajaxResult, response);
            }
        } else if (principal instanceof BaseSocialUser) {
            BaseSocialUser userDetail = (BaseSocialUser) authentication.getPrincipal();
            LoginUser loginUser = new LoginUser(new SysUser(Long.parseLong(userDetail.getUserId()), userDetail.getUsername(), userDetail.getPassword()), null,userDetail.getAuthorities(), null);
            //存储认证信息
            SecurityContextHolder.getContext().setAuthentication(authentication);
            //生成token
            String token = tokenService.createToken(loginUser);
            response.sendRedirect(url + "/login?token=" + token);
        }

    }
}

